package org.lintcode_Close.Stage1_Easy_50.O4字符串查找;

/**
 * @Auther: qingle
 * @Date: 2024/8/18-21:41
 * @Description:
 * @version: 1.0
 */
public class 两字符串和 {



	public String sumofTwoStrings(String a, String b) {
		// write your code here
		// 字符串转数字，再拼接
		char[] stringA = a.toCharArray();
		char[] stringB = b.toCharArray();

		int lenmax = Math.max(stringA.length, stringB.length);
		int cha = Math.abs(stringA.length - stringB.length);
		// 给字符串前面填充len长度的'0'
		StringBuilder stringBuilder = new StringBuilder();
		if (stringA.length < stringB.length) {
			for (int i = 0; i < cha; i++) {
				stringBuilder.append('0');
			}
			stringBuilder.append(stringA);

			stringA = stringBuilder.toString().toCharArray();
		}

		if (stringA.length > stringB.length) {
			for (int i = 0; i < cha; i++) {
				stringBuilder.append('0');
			}
			stringBuilder.append(stringB);

			stringB = stringBuilder.toString().toCharArray();
		}

		String[] stringC = new String[lenmax];

		for (int i = 0; i < lenmax; i++) {
			stringC[i] = String.valueOf(stringA[i] - '0' + stringB[i] - '0');
		}

		return String.join("", stringC);
	}

	public String sumofTwoStrings2(String a, String b) {
		// write your code here
		int alen = a.length();
		int blen = b.length();
		int i = alen-1;
		int j = blen-1;
		String res = "";
		while(i>=0 && j>=0){
			int anum = a.charAt(i)-'0';
			int bnum = b.charAt(j)-'0';
			int temp = anum + bnum;
			res = temp + res;
			i--;
			j--;
		}
		while(i>=0){
			int temp = a.charAt(i)-'0';
			res = temp + res;
			i--;
		}
		while(j>=0){
			int temp = b.charAt(j)-'0';
			res = temp + res;
			j--;
		}
		return res;

	}
	public static void main(String[] args) {

		System.out.println(new 两字符串和().sumofTwoStrings("123", "456"));
		System.out.println(new 两字符串和().sumofTwoStrings("1123", "456"));
		System.out.println(new 两字符串和().sumofTwoStrings("99", "111"));
		System.out.println(new 两字符串和().sumofTwoStrings2("99", "111"));
	}
}
